package utils;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.register.vo.GoodsVo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 程昱轩
 * @Description: TODO
 * @DateTime: @date 2023/3/2 14:37
 **/

public class JsoupUtils {
    public static List<GoodsVo> parsejd  (String keywords) throws Exception{
        List<GoodsVo> list = new ArrayList<>();
        //需要联网
        //
        //https://search.jd.com/Search?keyword=java
        String url="https://jiankang.163.com/";
        //解析网页 返回浏览器Document对象
        Document document = Jsoup.parse(new URL(url), 30000);
        //J_goodsList
        Element element = document.getElementById("navigation2");
        //System.out.println(element.html());
        Elements elements = element.getElementsByTag("li");
        for (Element e : elements) {
            GoodsVo goodsVo = new GoodsVo();
            //source-data-lazy-img
            String img = e.getElementsByTag("img").eq(0).attr("src");
            String title = e.getElementsByClass("inside_slide_mask").eq(0).text();
            if (StringUtils.isEmpty(img)) {
                continue;
            }
            if (StringUtils.isEmpty(title)) {
                continue;
            }
            goodsVo.setImg(img);
//            System.out.println(img);
//            System.out.println(price);
//            System.out.println(title+"----");
            goodsVo.setTitle(title);
            list.add(goodsVo);

        }
        return list;
    }
}
